package Model;

import Entity.Duration;
import Entity.DurationDetails;
import Manager.Manager;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author CONG
 */
public class DurationDetailsTableModel extends AbstractTableModel {

    private Manager manager;
    private String[] str = {"Due Date", "Paid Date", "Due Amount", "Paid Amount", "Fine Amount", "Finished"};
    
    public DurationDetailsTableModel(Manager manager) {
        this.manager = manager;
    }

    @Override
    public int getRowCount() {
        return manager.getDurationDetailsManager().getSize();
    }

    @Override
    public int getColumnCount() {
        return str.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        DurationDetails durde = manager.getDurationDetailsManager().getDurDe(rowIndex);
        SimpleDateFormat dateFormat=new SimpleDateFormat("MM/dd/yyyy");
        switch (columnIndex) {
            case 0:
                String dateDue=dateFormat.format(durde.getDateDue());
                return dateDue;
            case 1:
                if(durde.getPaidDate() == null) return null;
                else return dateFormat.format(durde.getPaidDate());
            case 2:
                return durde.getAmountDue();
            case 3:
                return durde.getAmountPaid();
            case 4:
                if(durde.getFineCode()==1)return 0;
                else if(durde.getFineCode()==2){
                    return durde.getAmountPaid()*2/100;
                }else if(durde.getFineCode()==3){
                    return durde.getAmountPaid()*5/100;
                }else if(durde.getFineCode()==4){
                    return durde.getAmountPaid()*8/100;
                }
            case 5:
                return durde.isStatus();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        return str[column];
    }

    public int getSize() {
        return manager.getDurationDetailsManager().getSize();
    }

    public DurationDetails getDurationDetails(int index) {
        return manager.getDurationDetailsManager().getDurDe(index);
    }

    public void getDurationDetailsList(ArrayList<DurationDetails> dur) {
        manager.getDurationDetailsManager().getDurationDetailsList(dur);
    }

    public void updateDurationHistory(DurationDetails durde){
         manager.getDurationDetailsManager().updateDurationHistory(durde);
         fireTableDataChanged();
    }
    public void removeDurationHistory(ArrayList<DurationDetails> durde){
        manager.getDurationDetailsManager().removeDurationDetails(durde);
        fireTableDataChanged();
    }
    public void addDurationHistory(DurationDetails durde){
        manager.getDurationDetailsManager().addDurationDetails(durde);
        fireTableDataChanged();
    }
}
